package com.shanghaionstar.nav.adapter;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.iview.ILoginListener;
import com.shanghaionstar.nav.presenter.NavSearchTypePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.LoginUtil;

import java.util.List;

/**
 * @author jun chen
 * @ClassName: NavSearchTypeFragmentAdapter
 * @date 2016年2月2日 下午2:41:57
 * @Description: 搜索类型页面的adapter
 */
public class NavSearchTypeFragmentAdapter extends CommonAdapter<PoiItemInfo> implements ILoginListener {

    private BaseActivity context;
    private List<PoiItemInfo> navSearchTypeInfoLists;
    private PoiItemInfo poiItemInfo;
    private int poiLength;
    private NavSearchTypePresenter navSearchTypePresenter;


    public NavSearchTypeFragmentAdapter(List<PoiItemInfo> datas, BaseActivity context, int poiLength, NavSearchTypePresenter navSearchTypePresenter) {
        super(datas, context);
        this.context = context;
        this.navSearchTypeInfoLists = datas;
        this.poiLength = poiLength;
        this.navSearchTypePresenter = navSearchTypePresenter;
    }

    public void setList(List<PoiItemInfo> datas) {
        this.navSearchTypeInfoLists = datas;
    }

    @Override
    public void getView(final int position, CommonViewHolder viewHolder) {
        final ViewHolder holder = (ViewHolder) viewHolder;
        poiItemInfo = new PoiItemInfo();
        poiItemInfo = navSearchTypeInfoLists.get(position);

        // 解决名称和地址超长问题
        String title = poiItemInfo.getTitle();
        if (!CommonUtil.isNull(title)) {
            holder.text_address_name.setText(title);
        } else {
            holder.text_address_name.setText("----");
        }

        if (poiItemInfo.getDistance() != 0) {
            holder.text_distance.setText(getDistance(poiItemInfo.getDistance()));
        } else {
            holder.text_distance.setText("");
        }

        String address = poiItemInfo.getAddress();
        if (!CommonUtil.isNull(address)) {
            holder.text_address_info.setText(address);
        } else {
            holder.text_address_info.setText("----");
        }

        String phone = "";
        if (poiItemInfo.getTelphone().contains(";")) {
            phone = poiItemInfo.getTelphone().split(";")[0];
        } else {
            phone = poiItemInfo.getTelphone();
        }
        if (CommonUtil.isNull(phone)) {
            holder.btn_call.setText(context.getResources().getString(R.string.nav_no_phone));
            holder.btn_call.setTextColor(context.getResources().getColor(R.color.hint_color));
            holder.btn_call.setClickable(false);
            holder.btn_call.setEnabled(false);
        } else {
            holder.btn_call.setText(phone);
            holder.btn_call.setTextColor(context.getResources().getColor(R.color.font_flow_price));
            holder.btn_call.setClickable(true);
            holder.btn_call.setEnabled(true);
        }

        //发送到车
        holder.btn_sendtocar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(context,
                        FunctionNameFindId.Categorysearch_navigation.getStringValue());
                Bundle bundle = new Bundle();
                bundle.putSerializable("poiSearchFav", navSearchTypeInfoLists.get(position));
                new LoginUtil().loginAndActionCallBack(context, RxConstants.NAV_SEARCH_FAV_PAGE, NavSearchTypeFragmentAdapter.this, bundle);
            }
        });

        holder.btn_call.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogUtil.showConfigureDialog(context, "", String.format(context.getString(R.string.call_number), holder.btn_call.getText().toString()), context.getString(R.string.btn_call), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        context.requestRunTimePermissions(new String[]{Manifest.permission.CALL_PHONE}, new PermissionCall() {
                            @Override
                            public void requestSuccess() {
                                BusinessUtil.sendReportByClickType(context,
                                        FunctionNameFindId.Categorysearch_call.getStringValue());
                                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + holder.btn_call.getText()));
                                context.startActivity(intent);

                            }

                            @Override
                            public void refused() {

                            }
                        });
                    }
                });
            }
        });
    }

    private String getDistance(int distance) {
        String disStr = "";
        if (distance < 1000) {
            disStr = distance + "米";
        } else {
            disStr = (float) distance / 1000f + "公里";
        }
        return disStr;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    @Override
    public int layoutId() {
        return R.layout.nav_search_type_list;
    }


    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        TextView text_address_name, text_distance;
        @FindViewById(R.id.text_address_info)
        TextView text_address_info;
        @FindViewById(R.id.btn_sendtocar)
        RippleCustomBtn btn_sendtocar;
        @FindViewById(R.id.line)
        View line;
        @FindViewById(R.id.btn_call)
        RippleCustomBtn btn_call;
    }

    @Override
    public void onLogin(Bundle bundle, String resultCode) {
        if (bundle != null) {
            if (RxConstants.ACTION_SUCCESS.equals(resultCode)) {
//                BusinessUtil.sendReportByClickType(context, FunctionNameFindId.Searchpoi_history_navigation.getStringValue());
                PoiItemInfo poiItemInfo = (PoiItemInfo) bundle.getSerializable("poiSearchFav");
                navSearchTypePresenter.sendTbtOrOdd(poiItemInfo);
            }
        }
    }
}
